<?php
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"></meta>
    <title>留言板</title>
</head>
<body>
<h3>留言列表</h3>
<input type="button" onclick="window.location.href='index.php'"
       value="首页"/>
<?php
if (!isset($_SESSION['username'])) {
    ?>
    <a href="login.php">登录</a>
    <?php
} else {
    if ($_SESSION['admin'] == "1") {
        echo "管理员，";
        echo "<a href='admin.php' title='管理面板'>", $_SESSION['username'], "</a>";
        echo " | ";
    } else {
        echo "欢迎您，";
        echo "<a href='admin.php' title='管理面板'>", $_SESSION['username'], "</a>";
        echo " | ";
    }
    ?>
    <a href="logout.php" onClick="return confirm('确定登出?');">登出</a>
    <?php
    if ($_SESSION['admin'] == "1") {
        echo '/';
        ?>
        <input type="button" onclick="window.location.href='sign.php'"
               value="注册子账号"/> /
        <input type="button" onclick="window.location.href='login.php'"
               value="替换账号"/>
        <?php
    }
}
?>

<p></p>
<?php
if (!isset($_POST['search'])) {
    if (!isset($_GET['searchtext'])) {
        ?>
        <form method="post" action="index.php">
            <input name="search" type="text" value=""/> <input type="submit"
                                                               name="submit" value="模糊搜索"/>
            <p></p>
        </form>
        <?php
    } else {
        ?>
        <form method="post" action="index.php">
            <input name="search" type="text" value="<?= $_GET['searchtext'] ?>"/> <input
                    type="submit" name="submit" value="模糊搜索"/>
            <p></p>
        </form>
        <?php
    }
} else {
    ?>
    <form method="post" action="index.php">
        <input name="search" type="text" value="<?= $_POST['search'] ?>"/> <input
                type="submit" name="submit" value="模糊搜索"/>
        <p></p>
    </form>
    <!-- <input type="button" onclick="window.location.href='admin.php'"
        value="留言管理"></input> -->
    <?php
}
// 引用相关文件
require './core/conn.php';
require './core/config.php';
if (!isset($_GET['p'])) {
    $p = 1;
} else {
    $p = $_GET['p'] ? $_GET['p'] : 1;
}
// 确定当前页数 $p 参数

// 数据指针
$offset = ($p - 1) * $pagesize;
if (!isset($_POST['search'])) {
    if (!isset($_GET['searchtext'])) {
        $searchtext = "";
    } else
        $searchtext = $_GET['searchtext'];
} else {
    $searchtext = $_POST['search'];
}
$query_sql = "SELECT * FROM msg WHERE `nickname` LIKE '%$searchtext%' OR `email` LIKE '%$searchtext%' OR `content` LIKE '%$searchtext%' OR `reply` LIKE '%$searchtext%' ORDER BY id DESC LIMIT  $offset , $pagesize";
$result = mysqli_query($conn, $query_sql);
// 如果出现错误并退出
if (!$result)
    exit('查询数据错误：' . mysqli_error());

// 循环输出
while (($gb_array = mysqli_fetch_array($result)) != false) {
    $content = nl2br($gb_array['content']);
    $nickname = $gb_array['nickname'];
    $ga_array = mysqli_fetch_array(mysqli_query($conn, "SELECT username, admin, himg FROM user WHERE `username` = '$nickname'"));
    ?>
        <img src="<?=$ga_array['himg']?>" alt="<?=$ga_array['username']?>" width="96" height="96"/>
<?php
    if ($ga_array['admin'] == "1") {
        if (!isset($_SESSION['username'])) {
            echo '管理员', '，';
            echo $gb_array['nickname'], '&nbsp;';
        } else {
            if ($ga_array['username'] == $_SESSION['username']) {
                echo '我', '，';
            } else {
                echo '管理员', '，';
                echo $gb_array['nickname'], '&nbsp;';
            }
        }
    } else {
        if (!isset($_SESSION['username'])) {
            echo '管理员', '，';
            echo $gb_array['nickname'], '&nbsp;';
        } else {

            if ($ga_array['username'] == $_SESSION['username']) {
                echo '我', '&nbsp;';
            } else {
                echo $gb_array['nickname'], '&nbsp;';
            }
        }
    }
    echo '发表于：' . date("Y-m-d H:i:s", $gb_array['createtime']) . '<br />';
    echo '内容：', nl2br($gb_array['content']), '<p></p>';
    if (!empty($gb_array['edittime'])) {
        echo '                    最后修改于：', date("Y-m-d H:i:s", $gb_array['edittime']), '<p></p>';
    }
    if (!empty($gb_array['replytime'])) {
        echo '----------------------------<br />';
        echo '管理员最后回复于：', date("Y-m-d H:i:s", $gb_array['replytime']), '<br />';
        echo '回复内容：', nl2br($gb_array['reply']), '<p></p>';
    }
    if (!isset($_SESSION['username'])) {
    } elseif ($gb_array['nickname'] == $_SESSION['username']) {
        ?>
        <form id="form1" name="form1" method="post" action="editmsg.php">
            <input name="id" type="hidden" value="<?= $gb_array['id'] ?>"/>
            <p>
                <input type="submit" name="submit" value="修改留言"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <a href="editmsg.php?action=delete&id=<?= $gb_array['id'] ?>"
                   onClick="return confirm('确定删除?');">删除留言</a>
            </p>
        </form>
        <?php
    } elseif ($_SESSION["admin"] == "1") {
        ?>
        <form id="form1" name="form1" method="post" action="replymsg.php">
            <input name="id" type="hidden" value="<?= $gb_array['id'] ?>"/> <input
                    type="submit" name="submit" value="回复留言"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="editmsg.php?action=delete&id=<?= $gb_array['id'] ?>"
               onClick="return confirm('确定删除?');">删除留言</a>
        </form>
        <?php
    }
    echo '<hr />';
}

// 计算留言页数
$count_result = mysqli_query($conn, "SELECT count(*) FROM msg WHERE `nickname` LIKE '%$searchtext%' OR `email` LIKE '%$searchtext%' OR `content` LIKE '%$searchtext%'");
$count_array = mysqli_fetch_array($count_result);
$pagenum = ceil($count_array['count(*)'] / $pagesize);
echo '共 ', $count_array['count(*)'], ' 条留言';
if (!isset($_GET['search'])) {
    if ($pagenum > 1) {
        for ($i = 1; $i <= $pagenum; $i++) {
            if ($i == $p) {
                echo '&nbsp;[', $i, ']';
            } else {
                if (!isset($_POST['search'])) {
                    if (!isset($_GET['searchtext'])) {
                        echo '&nbsp;<a href="index.php?p=', $i, '">' . $i . '</a>';
                    } else
                        echo '&nbsp;<a href="index.php?searchtext=', $_GET['searchtext'], '&p=', $i, '">' . $i . '</a>';
                } else {
                    echo '&nbsp;<a href="index.php?searchtext=', $_POST['search'], '&p=', $i, '">' . $i . '</a>';
                }
            }
        }
    }
} else {
    if ($pagenum > 1) {
        for ($i = 1; $i <= $pagenum; $i++) {
            if ($i == $p) {
                echo '&nbsp;[', $i, ']';
            } else {
                echo '&nbsp;<a href="index.php?search=', $_GET['search'], 'p=', $i, '">' . $i . '</a>';
            }
        }
    }
}
?>


<p></p>
<input type="button" onclick="window.location.href='add.php'"
       value="点击留言"/>

</body>
</html>